package com.lz.fruitday.fragment;


import android.content.Intent;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.lz.fruitday.BaseApp;
import com.lz.fruitday.R;
import com.lz.fruitday.adapter.AdPagerAdapter;
import com.lz.fruitday.adapter.HomeLvAdapter;
import com.lz.fruitday.adapter.QXAdapter;
import com.lz.fruitday.bean.AppBaseBean;
import com.lz.fruitday.bean.QiangxianBaseBean;
import com.lz.fruitday.bean.QiangxianProduct;
import com.lz.fruitday.bean.RotationBean;
import com.lz.fruitday.constant.Path;
import com.lz.fruitday.ui.QiangxianActivity;
import com.lz.fruitday.utils.ImageLoaderUtil;
import com.lz.fruitday.widget.GridViewForScrollView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment {

    @ViewInject(R.id.lv_home)
    private ListView lvHome;
    private ArrayList<RotationBean> beans=new ArrayList<>();
    private HomeLvAdapter lvAdapter;
    private  AsyncHttpClient client = new AsyncHttpClient();
    //顶部广告
    private View headView;
    private ArrayList<RotationBean> rotationBeans =new ArrayList<>();
    private ArrayList<ImageView> imageViews=new ArrayList<>();
    private AdPagerAdapter adPagerAdapter;
    private ViewPager vpAd;
    private DisplayImageOptions options= ImageLoaderUtil.getDefaultOptions();
    //Gridview相关
    private GridViewForScrollView headGv;
    private String[] names={"天天到家","抢鲜啦！","摇一摇","充值有礼","物流售后","提货卷","企业团购","免费试吃"};
    private int[] images={R.mipmap.icon_juice,R.mipmap.icon_flash,R.mipmap.icon_shake,R.mipmap.icon_topup,R.mipmap.home_order,R.mipmap.redemption,R.mipmap.enterprise,R.mipmap.icon_eattry};
    //上面listview
    private ListView lvTop;
    private ArrayList<RotationBean> topBanners=new ArrayList<>();
    private HomeLvAdapter lvTopAdapter;
    //中间gridview
    private GridViewForScrollView midGv;
    private ArrayList<RotationBean> horizontalBanner =new ArrayList<>();
    private HomeLvAdapter gvAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        ViewUtils.inject(this, view);
        //添加头布局
        addHead(inflater);
        initView();
        initData();
        return view;
    }

    private void initData() {
        client.post(Path.PATH_HOME_PAGE, getParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<AppBaseBean<RotationBean>> typeReference = new TypeReference<AppBaseBean<RotationBean>>() {
                };
                AppBaseBean baseBean = JSONObject.parseObject(responseString, typeReference);
                beans.addAll(baseBean.getBanner());
                lvAdapter.notifyDataSetChanged();
                //广告数据
                rotationBeans.addAll(baseBean.getRotation());
                for (int i = 0; i < rotationBeans.size(); i++) {
                    ImageView iv = new ImageView(getActivity());
                    iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    ImageLoader.getInstance().displayImage(rotationBeans.get(i).getPhoto(), iv, options);
                    imageViews.add(iv);
                }
                adPagerAdapter.notifyDataSetChanged();
                //上面listview
                topBanners.addAll(baseBean.getTop_banner());
                lvTopAdapter.notifyDataSetChanged();
                //中间gridview
                horizontalBanner.addAll(baseBean.getHorizontal_product_banner());
                gvAdapter.notifyDataSetChanged();

            }
        });
    }
    private RequestParams getParams() {
        RequestParams params = new RequestParams();
        params.add("sign", "a286d46bbaec46a42298e95b185fbf3a");
        params.add("timestamp", "1438873575");
        params.add("platform", "ANDROID");
        params.add("source", "app");
        params.add("connect_id", "");
        params.add("region_id", "143949");
        params.add("service", "marketing.banner");
        params.add("device_id", "93789e8978f2eefdcf52006b4d96ee23");
        params.add("channel", "qq");
        params.add("version", "2.3.0");
        return params;
    }

    private void initView() {
        lvHome.addHeaderView(headView);
        lvAdapter=new HomeLvAdapter(beans,getActivity());
        lvHome.setAdapter(lvAdapter);
    }

    private void addHead(LayoutInflater inflater) {
        headView = inflater.inflate(R.layout.item_add_home_layout,null);
        //头部广告
        vpAd = (ViewPager) headView.findViewById(R.id.home_head_viewpager);
        adPagerAdapter = new AdPagerAdapter(imageViews);
        vpAd.setAdapter(adPagerAdapter);
        //上面gridview
        initGridview();
        headGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (images[position]){
                    case R.mipmap.icon_flash:
                        startActivity(new Intent(getActivity(),QiangxianActivity.class));
                        break;
                }
            }
        });
        //上面listview
        lvTop = (ListView) headView.findViewById(R.id.lv_top_banner);
        lvTopAdapter = new HomeLvAdapter(topBanners,getActivity());
        lvTop.setAdapter(lvTopAdapter);
        //中间gridview
        midGv = (GridViewForScrollView) headView.findViewById(R.id.home_head2_gv);
        gvAdapter = new HomeLvAdapter(horizontalBanner,getActivity());
        midGv.setAdapter(gvAdapter);
    }
    private void initGridview() {
        headGv= (GridViewForScrollView) headView.findViewById(R.id.home_head_gv);
        ArrayList<Map<String,Object>> gvList=new ArrayList<Map<String,Object>>();
        for (int i = 0; i < names.length; i++) {
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("image",images[i]);
            map.put("name",names[i]);
            gvList.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(getActivity(),gvList,R.layout.item_gridview_layout,new String[]{"image","name"},new int[]{R.id.image,R.id.name});
        headGv.setAdapter(adapter);
    }

}
